package com.wyh.spring.demo.controller;

import com.wyh.spring.demo.model.MessageInfo;
import com.wyh.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 留言版后端代码
 * User: 王圆豪
 * Date: 2025-07-01
 * Time: 23:34
 */

@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;
//    private List<MessageInfo> l = new ArrayList<>();

    @GetMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.selectAll();
    }

    @PostMapping(value = "/publish",produces = "application/json")
    public String publish(@RequestBody MessageInfo messageInfo){
        if (!StringUtils.hasText(messageInfo.getMessage()) ||
                !StringUtils.hasText(messageInfo.getFrom()) ||
                !StringUtils.hasText(messageInfo.getTo())){
            return "{\"ok\": 0}";
        }
        messageService.insertMessage(messageInfo);
        return "{\"ok\": 1}";
    }
}
